package net.jzysoft.app.ui.Login;

import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import net.jzysoft.app.Base.BaseActivity;
import net.jzysoft.app.R;
import net.jzysoft.app.ui.MainActivity;
import net.jzysoft.app.utils.CommUtility;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class Login extends BaseActivity implements View.OnClickListener{
    public static boolean loginFailed = false;
    @BindView(R.id.lg_et_phone)
    EditText lgEtPhone;
    @BindView(R.id.lg_et_password)
    EditText lgEtPassword;
    @BindView(R.id.lg_img_password_eye)
    ImageView lgImgPasswordEye;
    @BindView(R.id.btn_log)
    Button btnLog;
    @BindView(R.id.btn_zhuce)
    Button btnZhuce;
    private long mBackPressedTime;
    private boolean isDisplayPassword;//是否显示密码
    private Button btn_log;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        btn_log = (Button) findViewById(R.id.btn_log);
        btn_log.setOnClickListener(this);
    }

    private void displayPassword() {
        CommUtility.displayPassword(lgEtPassword, lgImgPasswordEye, isDisplayPassword);
        isDisplayPassword = !isDisplayPassword;
    }
    @OnClick({R.id.btn_log, R.id.btn_zhuce,R.id.lg_img_password_eye})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_log:
                if(TextUtils.isEmpty(lgEtPhone.getText())){
                    Toast.makeText(Login.this, "请输入账号", Toast.LENGTH_SHORT).show();
                    return;
                }
                if(TextUtils.isEmpty(lgEtPassword.getText())){
                    Toast.makeText(Login.this, "请输入密码", Toast.LENGTH_SHORT).show();
                    return;
                }
              if(TextUtils.equals(lgEtPhone.getText(),"123456")){
                  Intent intent = new Intent(Login.this, MainActivity.class);
                  startActivity(intent);
              }else {
                  Toast.makeText(Login.this, "账号密码错误", Toast.LENGTH_SHORT).show();
              }
                Intent intent=new Intent(Login.this,MainActivity.class);
                startActivity(intent);
                finish();
                break;
            case R.id.btn_zhuce:
                break;
            case R.id.lg_img_password_eye:
                displayPassword();
                break;
        }
    }
    @Override
    public void onBackPressed() {

        boolean isDoubleClick = true;// BaseApplication.get(AppConfig.KEY_DOUBLE_CLICK_EXIT, true);

        if (isDoubleClick) {
            long curTime = SystemClock.uptimeMillis();
            if ((curTime - mBackPressedTime) < (3 * 1000)) {
                finish();
            } else {
                mBackPressedTime = curTime;
                Toast.makeText(this, "再次点击退出", Toast.LENGTH_LONG).show();
            }
        } else {
            finish();
        }
    }

    // 根据EditText所在坐标和用户点击的坐标相对比，来判断是否隐藏键盘
    private boolean isShouldHideKeyboard(View v, MotionEvent event) {
        if (v != null && (v instanceof EditText)) {
            int[] l = {0, 0};
            v.getLocationInWindow(l);
            int left = l[0],
                    top = l[1],
                    bottom = top + v.getHeight(),
                    right = left + v.getWidth();
            return !(event.getX() > left && event.getX() < right
                    && event.getY() > top && event.getY() < bottom);
        }
        return false;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_log:
                Intent intent=new Intent(Login.this,MainActivity.class);
                startActivity(intent);
                finish();
                break;
        }
    }
}
